﻿package com.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.BaseController;
import com.entity.Production;
import com.service.ProductionService;

@Controller
@RequestMapping("/admin/product")
public class AdminProductController extends BaseController {
	
	@Autowired
	private ProductionService productionService;
	
	@RequestMapping("/product-main")
	public String index(){
		return "admin.product.index";
	}
	
	@RequestMapping("/list")
	public String listByCategory(HttpServletRequest request, HttpServletResponse response){
		String categoryId = request.getParameter("id");
		int start = this.getStart(request);
		int limit = this.getLimit(request);
		List<Production> list = this.productionService.findByCategoryInPage(categoryId, start, limit);
		JSONArray array = new JSONArray();
		for(Production pro:list){
			JSONObject obj = new JSONObject();
			obj.put("id", pro.getId());
			obj.put("product_name", pro.getProductName());
			obj.put("product_editor", pro.getAdmin().getRealName());
			obj.put("insert_time", pro.getInsertTime());
			obj.put("product_views", pro.getProductViews());
			obj.put("is_hot", pro.getIsHot());
			obj.put("is_top", pro.getIsTop());
			obj.put("product_image", pro.getOneImage());
			array.add(obj);
		}
		String json = this.genListJson(this.productionService.countByCategory(categoryId), array);
		this.outJson(json, response);
		return null;
	}
}
